<?php
/*************************************************************************************
 *
 *	FileName: /edp/module/ajax_collect/header_php.php
 *	Desc    : This file is to collect .
 *	Author  : Bo
 *	Date    : 2009-11-6
 *
 *************************************************************************************/
    
	$userID=GetLoginUserID();
	$userName=GetLoginUserName();
	
	if( !IsParaOK() )
	{
		echo_validation_message();
		edp_exit();
	}
	
	if($userID==false||$userName==false)
	{
		$_GET['login']=false;
		echo(json_encode($_GET));
		edp_exit();
	}
	else
	{
		$type=$_GET['type'];
		$tiny=$_GET['tiny'];
		$collectID=intval($_POST['collectID']);
		if(isset($_POST['action']))
		$action=trim($_POST['action']);
		else 
		$action = '';
		
		$collectionDAO=GetCollectionDAO();
		//Collect Shop
		if($type=='shop')
		{
			//step 1
			if($tiny=='1')
			{
				$_GET['removeBtn']="false";
				if($collectID>0)
				{
					$shop = $collectionDAO->GetCollectionShopBySid($userID, $collectID);	 		  
		    		if($shop)
		    		{  	
		    			$_GET['removeBtn']="true";
		    			$_GET['favTags']=$shop['tags'];
					}
					//get ordered top 10 tags
					$favTagsList=$collectionDAO->GetShopTagsByUid($userID, '1', '10');
					if($favTagsList)
					{
						$favTagStr='';
						foreach($favTagsList as $index=>$tag)
						{
							$favTagStr=$favTagStr.'<a href="#">'.$tag.'</a>'.' ';
						}	
						$_GET['favTagsList'] = $favTagStr;	
					}	 
				}
				echo( json_encode($_GET) ); 
			}
			else
			//step 2
			if($tiny=='2')
			{
				if(($action=='collect')&&($collectID>'0'))
				{
					$tags = edp_db_input(trim($_POST['favTags']));
					if($collectionDAO->CollectShop($userID,$collectID,$tags)==true)
					{
						$eventDispatcher = GetEventDispatcher();
						$para['sid']=$collectID;
						$eventDispatcher->Notify( $userID, COLLECT_SHOP, $para );
					}
				}
				else if(($action=='delete')&&($collectID>'0'))
					$collectionDAO->DeleteShop($userID,$collectID);
			}
		}
		else
		//Collect product
		if($type=='product')
		{
			//step 1
			if($tiny=='1')
			{
				$_GET['removeBtn']="false";	
				if($collectID>'0')
				{
					$product=$collectionDAO->GetCollectionProductByPid($userID,$collectID); 
					if($product)
					{  	
						$_GET['removeBtn']="true";
						$_GET['favTags']=$product['tags'];
					}
				
					//get ordered top 10 tags
				    $favTagsList=$collectionDAO->GetProductTagsByUid($userID, '1', '10');  
					if($favTagsList)
					{
						$favTagStr='';
						foreach($favTagsList as $index=>$tag)
						{
							$favTagStr=$favTagStr.'<a href="#">'.$tag.'</a>'.' ';
						}	
						$_GET['favTagsList'] = $favTagStr;	
					 }	 			  
				}
				echo( json_encode($_GET) ); 
			}
			else
			//step 2
			if($tiny=='2')
			{
				if($action =='collect'&&($collectID>'0'))
				{
					$tags = edp_db_input(trim($_POST['favTags']));
			  		if($collectionDAO->CollectProduct($userID,$collectID,$tags)==true)
			  		{
				  		$eventDispatcher = GetEventDispatcher();
				  		$para['pid']=$collectID;
				  		$productDAO=GetProductDAO();
				  		$product_info=$productDAO->GetProductInfo($collectID);
				  		$para['sid']=$product_info['shop_id'];
						$eventDispatcher->Notify( $userID, COLLECT_PRODUCT, $para );	
			  		}	  		
				}
				elseif($action == 'delete'&&($collectID>0))
					$collectionDAO->DeleteProduct($userID,$collectID);	
			}
		}
		else
		//Collect list
		if($type=='list')
		{
			//step 1
			if($tiny=='1')
			{
				$_GET['removeBtn']="false";	
			
				if($collectID>0)
				{					
					$list = $collectionDAO->GetCollectionListByLid($userID, $collectID);
					if($list)
					{  	
						$_GET['removeBtn']="true";
						$_GET['favTags']=$list['tags'];
					}
					//get ordered top 10 tags
					$favTagsList = $collectionDAO->GetListTagsByUid($userID);  
					if($favTagsList)
					{
						$favTagStr='';
						foreach($favTagsList as $index=>$tag)
						{
							$favTagStr=$favTagStr.'<a href="#">'.$tag.'</a>'.' ';
						}	
						$_GET['favTagsList'] = $favTagStr;	
					 }	 		 	
				}
				echo( json_encode($_GET) ); 
			}
			else
			//step 2
			if($tiny=='2')
			{
				if($action =='collect'&&($collectID>0))
				{
					$tags = edp_db_input(trim($_POST['favTags']));
			  		if($collectionDAO->CollectList($userID,$collectID,$tags)==true)
			  		{
				  		$eventDispatcher = GetEventDispatcher();
				  		$para['lid']=$collectID;
						$eventDispatcher->Notify( $userID, COLLECT_LIST, $para );	
			  		}	  				  		
				}
				elseif($action == 'delete'&&($collectID>'0'))
					$collectionDAO->DeleteList($userID,$collectID);	
			}
		}
	}
?>